Skip to content

Apply MediaElement.Speed when playback starts on iOS and fix inverted AreFloatingPointNumbersEqual - #3327

Merged
TheCodeTraveler merged 8 commits into
CommunityToolkit:mainfrom
ne0rrmatrix:FixIosMediaElementSpeedOnPlay
Oct 4, 2026
Merged

TheCodeTraveler merged 8 commits into
CommunityToolkit:mainfrom
ne0rrmatrix:FixIosMediaElementSpeedOnPlay

Conversation

@ne0rrmatrix

@ne0rrmatrix ne0rrmatrix commented Oct 1, 2026 •

Copy link
Copy Markdown
Member

Description of Change

Fixes two related defects in MediaElement described in #3326.

1. iOS / Mac Catalyst: playback starts at 1x regardless of Speed

AVPlayer.Play() always starts playback at a rate of 1. MediaManager.macios.cs called it from PlatformPlay, from the auto-play path in PlatformUpdateSource and from PlayedToEnd when looping, so each of those reset playback to 1x while MediaElement.Speed kept the requested value.

These three call sites now use a new PlayAtSpeed() helper, which starts playback by setting Player.Rate to MediaElement.Speed (and falls back to Player.Play() when Speed is 0).

2. MediaManager.AreFloatingPointNumbersEqual was inverted

It returned Math.Abs(number1 - number2) > tolerance, i.e. true when the numbers differ. It now returns true when they are within the tolerance. Call sites:

Call site Before After
iOS RateChanged Never copied the player's rate into Speed Copies it when it differs. A rate of 0 is ignored, because AVPlayer reports 0 while paused and that is not a change of the requested speed
Windows OnPlaybackSessionPlaybackRateChanged Correct, because it relied on the inverted result Negated, so behavior is unchanged
Android OnPlaybackParametersChanged Never updated Speed Updates Speed when the player's speed differs
Android PlatformUpdateShouldMute Refreshed volumeBeforeMute only when it already equalled Player.Volume, so a Volume set before the handler connected was overwritten with 1 Refreshes it when it differs, so the initial Volume is kept

Linked Issues

PR Checklist

  • Has a linked Issue, and the Issue has been approved(bug) or Championed (feature/proposal) — [BUG] iOS: MediaElement plays at 1x after Play(), auto-play or loop while Speed reports the requested value #3326 is linked and not yet approved
  • Has tests (if omitted, state reason in description) — unit tests added for AreFloatingPointNumbersEqual. The iOS and Android playback changes have no automated test; they were verified on a simulator and an emulator, see below
  • Has samples (if omitted, state reason in description) — n/a, bug fix with no new API
  • Rebased on top of main at time of PR
  • Changes adhere to coding standard
  • Documentation created or updated — n/a, no API change

Additional information

Verification

The same scripted scenarios were run against main (c8740a8) and this branch, using a small test app that references the MediaElement project and logs state, position and Speed.

iOS (iPhone 17 simulator, iOS 26.5), 24.87s local MP4 unless noted:

Scenario main This PR
ShouldAutoPlay = false, Speed = 2, then Play(); position after 3s 2.81s 5.84s
ShouldAutoPlay = false, Speed = 1.5, then Play() (remote MP4); position after 3s 2.80s 4.10s
Playing at Speed = 2, Pause(), Play(); media played in the next 2s 1.91s 3.75s
Playing at Speed = 2, Stop(), Play(); position after 2s 1.83s 3.75s
ShouldAutoPlay = true with initial Speed = 2; position after 2s 1.76s 3.54s
ShouldLoopPlayback = true at Speed = 2 (3.89s MP3); time per loop about 4s about 2s

Speed reported 2 (or 1.5) throughout on both builds. Pause() leaves Speed unchanged with this PR. Fourteen further scenarios (auto-play on and off, source changes, invalid URL, HLS, audio, Source = null, play to end) produce the same event and state sequence on both builds, apart from the 2x auto-play case now reaching MediaEnded sooner.

Android (Android 17 emulator), one run per build:

  • Speed change, pause / resume, Speed = 0 then Speed = 1.5, and mute / unmute with volume changes behave identically on both builds.
  • A MediaElement created with Volume = 0.3 reports Volume 1.00 on main and 0.30 with this PR.

Builds and tests (.NET SDK 10.0.401, Xcode 26.6): CommunityToolkit.Maui.MediaElement builds in Release for net10.0, Android, iOS and Mac Catalyst with no warnings from the changed files; CommunityToolkit.Maui.UnitTests passes (11,679 tests, including the 15 added here).

Not verified

  • Windows: the one-line change is a negation that preserves existing behavior, but it was neither compiled nor run (no Windows machine).
  • Mac Catalyst: compiles, not run.
  • Physical devices.
  • The iOS rate-to-Speed sync driven by the native player controls' speed menu, which this PR makes functional, was not exercised by hand.

Notes for reviewers

  • AreFloatingPointNumbersEqual is public static, so the corrected return value is visible to anyone calling it directly.
  • Overlap with Fix ios media element start bug #3281: see the section below.
  • Not changed here: on iOS, setting Speed while paused starts playback, because PlatformUpdateSpeed assigns Player.Rate directly. That is existing behavior on main.

Overlap with #3281

#3281 also edits MediaManager.macios.cs. The two PRs touch different things except for one spot: the auto-play start. On main that call lives in PlatformUpdateSource; this PR changes it in place to PlayAtSpeed(), and #3281 moves it into a new PlayerItemStatusChanged method that runs after ReadyToPlay. Git will report a conflict there for whichever PR merges second.

Resolution, in either order: keep #3281's structure and start playback with this PR's helper, so the block in PlayerItemStatusChanged reads:

if (MediaElement.ShouldAutoPlay)
{
    PlayAtSpeed();
}

Verified locally (iPhone 17 simulator, iOS 26.5). Both branches were merged onto main (c8740a8) with the resolution above:

The replacement is required, not optional. Resolving the conflict by keeping the Player.Play(); Player.Rate = (float)MediaElement.Speed; pair was also tested: with auto-play and Speed = 2, Speed is overwritten with 1 and playback runs at 1x (3.03s after 4s). #3327 makes RateChanged copy the player's rate into MediaElement.Speed, and Player.Play() sets the rate to 1 before the pair reapplies it.

The other changes here (PlatformPlay, the loop restart, RateChanged, the helper and its Windows call site) do not overlap with #3281.

🤖 Generated with Claude Code

… AreFloatingPointNumbersEqual

AVPlayer.Play() always starts playback at a rate of 1, so Play(), auto-play
and loop restarts ignored MediaElement.Speed on iOS and Mac Catalyst. They now
start playback by setting the rate to the requested Speed.

AreFloatingPointNumbersEqual returned true when the numbers differed. It now
returns true when they are within the tolerance. The Windows call site is
negated to keep its behavior; the iOS RateChanged handler now ignores the
rate of 0 that AVPlayer reports while paused.

Fixes CommunityToolkit#3326

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 1, 2026 21:27
@ne0rrmatrix ne0rrmatrix mentioned this pull request Oct 1, 2026
4 of 10 tasks

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Now Playing metadata updates can be skipped, and negative playback speeds incorrectly fall back to 1x.

Review effort: Balanced
Findings: 2 Medium severity

Open (2)
What changed in this PR

Addresses #3326 by applying the requested MediaElement playback speed on Apple platforms and correcting the shared floating-point comparison.

Changes:

  • Applies playback speed when playing, auto-playing, and looping.
  • Corrects equality semantics while preserving Windows rate synchronization.
  • Adds 15 comparison test cases.
File Description
src/​CommunityToolkit.Maui.UnitTests/​Views/​MediaElement/​MediaManagerTests.cs Tests default and custom tolerances.
src/​CommunityToolkit.Maui.MediaElement/​Views/​MediaManager.windows.cs Preserves rate synchronization after the comparison fix.
src/​CommunityToolkit.Maui.MediaElement/​Views/​MediaManager.shared.cs Corrects floating-point equality.
src/​CommunityToolkit.Maui.MediaElement/​Views/​MediaManager.macios.cs Applies playback speed and updates rate synchronization.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

…rting playback

RateChanged now guards only the Speed assignment with the equality check, so
a nonzero rate change always updates NowPlayingInfo.PlaybackRate, as it did
before the helper was corrected.

PlayAtSpeed falls back to Player.Play() only when Speed is exactly 0, so a
negative Speed is applied instead of restarting forward at 1x.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

Native-control synchronization has an unresolved remote-resume regression, and cross-platform verification remains incomplete.

Review effort: Balanced
Findings: None

Resolved since last review (2)
Previously missed (1)

In code that hasn't changed since last review

Medium severity Remote Play ignores the configured playback speed

src/​CommunityToolkit.Maui.MediaElement/​Views/​MediaManager.macios.cs:827

Lock-screen and Control Center Play still bypass this helper: Metadata.PlayCommand calls player.Play() (Primitives/Metadata.macios.cs:151), which starts at rate 1. The corrected comparison in RateChanged now copies that rate into MediaElement.Speed, so resuming with Speed = 2 loses the requested speed; subsequent application Play() calls also use 1 unless the app resets Speed. Pass a speed-aware playback callback into Metadata and use it for remote Play, while retaining synchronization for genuine native speed changes.

Lock screen and Control Center Play called AVPlayer.Play(), which always
starts at a rate of 1, and RateChanged then copied that rate back into
MediaElement.Speed. Route the remote Play and toggle commands through
PlayAtSpeed instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ne0rrmatrix

Copy link
Copy Markdown
Member Author

Re: Copilot review, "Remote Play ignores the configured playback speed".

Valid, fixed in 29938a0. Metadata now takes a speed-aware play callback and MediaManager passes PlayAtSpeed to it, so the remote Play command (and the play branch of the toggle command) no longer calls player.Play() and starts at the requested Speed. RateChanged is unchanged, so genuine native speed changes still sync back to MediaElement.Speed.

Verified on a physical iPad Air 13-inch (M4) with Speed = 2, sending the system remote Play command while paused and measuring how fast the position advanced:

Step Before After
Pause, then remote Play 1.00x, Speed reset to 1 2.00x, Speed 2
App Pause() + Play() afterwards 1.00x, Speed 1 2.00x, Speed 2
Remote Pause, then remote Play 1.00x, Speed 1 2.00x, Speed 2

TheCodeTraveler and others added 2 commits October 1, 2026 18:17
MediaManager.macios.cs: keep main's early return in PlatformUpdateSource;
the PR's MediaOpened/GetVideoDimensions/PlayAtSpeed/SetPoster block there
is superseded by CommunityToolkit#3281's PlayerItemStatusChanged.
Autoplay in PlayerItemStatusChanged now calls PlayAtSpeed() instead of
Play() then Rate = speed, whose two rate changes made RateChanged flip
Speed between 1 and the requested value and stall playback.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
RateChanged copies the player's rate into MediaElement.Speed, and
PlatformUpdateSpeed sent that value back to the player as a new rate
request. When two rates are set in one main-loop turn, AVPlayer reports
the earlier rate again afterwards, so the two requests kept replacing each
other: Speed flipped about every 40 ms and playback stalled. This happened
when Speed was set in a MediaOpened handler of an autoplaying element
(autoplay starts in the same turn), or when Speed changed twice, or Play()
and a Speed change ran, in one main-loop turn. PlatformUpdateSpeed now
skips the update when the player already runs at the requested Speed.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

Unresolved native playback defects and incomplete platform verification require fixes and human review.

Review effort: Balanced
Findings: 1 High severity · 1 Medium severity

Open (2)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Unresolved playback correctness and object-retention issues block approval.

Review effort: Balanced
Findings: 2 Medium severity

Open (2)
Resolved since last review (2)

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

@TheCodeTraveler TheCodeTraveler left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks James!

@TheCodeTraveler
TheCodeTraveler merged commit 70f48c5 into CommunityToolkit:main Oct 4, 2026
10 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 6, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] iOS: MediaElement plays at 1x after Play(), auto-play or loop while Speed reports the requested value

3 participants